package com.chenjun.core.auth;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;

/**
 * 对当前登录用户的操作权限进行过滤
 * 如果当前用户有权限访问此资源，则返回为true
 * 否则返回false
 * 
 * @since 0.9
 */
public class AnyOfRolesAuthorizationFilter extends RolesAuthorizationFilter {
	
	private static final Logger logger = Logger.getLogger(AnyOfRolesAuthorizationFilter.class);

	public AnyOfRolesAuthorizationFilter() {
	}

	@Override
	public boolean isAccessAllowed(ServletRequest request, ServletResponse response, final Object mappedValue) throws IOException {
		
//		log.info("----AnyOfRolesAuthorizationFilter-isAccessAllowed----");

		final Subject subject = getSubject(request, response);
		final String[] rolesArray = (String[]) mappedValue;

		if (rolesArray == null || rolesArray.length == 0) {
			//no roles specified, so nothing to check - allow access.
			return true;
		}

		for (final String roleId : rolesArray) {
			if (subject.hasRole(roleId)) {
				return true;
			}
		}

		return false;
	}
}
